﻿using System;
using System.Collections.Generic;
using System.Text;
using Template.Infrastructure.Filter;
using System.Linq;
using Template.Infrastructure.Extensions;

namespace Template.Infrastructure.Reflection
{
    /// <summary>
    ///  指定基类的实现类型查找器基类
    /// </summary>
    /// <typeparam name="TBaseType">要查找类型的基类</typeparam>
    public abstract class BaseTypeFinderBase<TBaseType> : FinderBase<Type>, ITypeFinder
    {

        private readonly IAllAssemblyFinder _allAssemblyFinder;

        protected BaseTypeFinderBase(IAllAssemblyFinder allAssemblyFinder)
        {
            _allAssemblyFinder = allAssemblyFinder;
        }

        /// <summary>
        /// 重写以实现所有项的查找
        /// </summary>
        /// <returns></returns>
        protected override Type[] FindAllItems()
        {
           var assemblys=   _allAssemblyFinder.FindAll(true);
           return assemblys.SelectMany(assembly => assembly.GetTypes()).Where(o => o.IsDeriveClassFrom<TBaseType>()).Distinct().ToArray();
        }
    }
}
